package great.easychat.help;

import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;

import great.easychat.help.util.DebugLog;
import great.easychat.help.util.ToastUtil;
import great.easychat.help.viewModel.CommonViewModel;

public class WebUrlActivity extends BaseActivity<CommonViewModel> {

    WebView webView;

    @Override
    protected void observeLiveData() {

    }

    @Override
    protected void setViewData(Bundle savedInstanceState) {
        webView = findViewById(R.id.webView);
        String url = getIntent().getStringExtra("web_url");
        String title = getIntent().getStringExtra("title");
        setTitle(title);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAllowFileAccess(false);
        webView.getSettings().setAllowFileAccessFromFileURLs(false);
        webView.getSettings().setTextZoom(100);
        webView.getSettings().setSavePassword(false);
        webView.getSettings().setAllowUniversalAccessFromFileURLs(false);
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        // 开启 DOM storage API 功能
        webView.getSettings().setDomStorageEnabled(true);
        // 开启 database storage API 功能
        webView.getSettings().setDatabaseEnabled(true);
        webView.getSettings().setAppCacheEnabled(true);
        String cacheDirPath = getCacheDir().getAbsolutePath() + File.separator + "web_cacha";
        Log.i("cachePath", cacheDirPath);
        // 设置数据库缓存路径
        webView.getSettings().setAppCachePath(cacheDirPath);
        webView.getSettings().setSupportZoom(true);
        //支持缓存
        webView.getSettings().setAppCacheEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url == null) {
                    return false;
                }
                try {
                    if (url.startsWith("weixin://") || url.startsWith("alipays://") || url.startsWith("mailto://") || url.startsWith("tel://")) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                        return true;
                    }
                    if (url.startsWith("tbopen://")) {
                        // dns 劫持，会被定位到淘宝上，这边拦截下
                        ToastUtil.show("网络异常请重新连接");
                        return false;
                    }
                } catch (Exception e) { //防止crash (如果手机上没有安装处理某个scheme开头的url的APP, 会导致crash)
                    ToastUtil.show("您还没有安装支付宝客户端");
                    return false;
                }
                return false;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {

            }
        });
        webView.loadUrl(url);
    }

    @Override
    protected int requestLayoutId() {
        return R.layout.activity_web_url;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public static void start(Activity activity, String url, String title) {
        Intent intent = new Intent(activity, WebUrlActivity.class);
        intent.putExtra("web_url", url);
        intent.putExtra("title", title);
        activity.startActivity(intent);
    }

    public static final int REQUEST = 1001;

    public static void startFor(Activity activity, String url, String title) {
        Intent intent = new Intent(activity, WebUrlActivity.class);
        intent.putExtra("web_url", url);
        intent.putExtra("title", title);
        activity.startActivityForResult(intent, REQUEST);
    }

}
